package mobility;

import java.sql.ResultSet;
import java.sql.SQLException;

public class Clientes {
	private String nic;
	private String longitud;
	private String latitud;
	private db conexion = null;
	
	public String getNic() {
		return nic;
	}
	public void setNic(String nic) {
		this.nic = nic;
	}
	
	public String getLongitud() {
		return longitud;
	}
	public void setLongitud(String longitud) {
		this.longitud = longitud;
	}
	public String getLatitud() {
		return latitud;
	}
	public void setLatitud(String latitud) {
		this.latitud = latitud;
	}
	
	public db getConexion() {
		return conexion;
	}
	public void setConexion(db conexion) {
		this.conexion = conexion;
	}
	public Clientes(db conexion) {
		super();
		this.conexion = conexion;
	}
	
	public boolean add() throws SQLException{
		boolean result = false;
			String sql = "insert into clientes (nic,latitud,longitud) values (?,?,?)";
			java.sql.PreparedStatement pst =  this.getConexion().getConnection().prepareStatement(sql);
			pst.setString(1, this.nic);
			pst.setString(2, this.latitud);
			pst.setString(3, this.longitud);
			if (conexion.Update(pst) > 0) {
				conexion.Commit();
				result = true;
			}
		return result;
	}
	public boolean modify(String key) throws SQLException{
		boolean result = false;
			String sql = "update clientes set nic=?, latitud =?, longitud=? where nic=?";
			java.sql.PreparedStatement pst =  this.getConexion().getConnection().prepareStatement(sql);
			pst.setString(1, this.nic);
			pst.setString(2, this.latitud);
			pst.setString(3,this.longitud);
			pst.setString(4,key);
			if (conexion.Update(pst) > 0) {
				conexion.Commit();
				result = true;
			}
		return result;
	}
	public boolean remove(String key) throws SQLException {
		boolean result = false;

			String sql = "delete from clientes where nic=?";
			java.sql.PreparedStatement pst =  this.getConexion().getConnection().prepareStatement(sql);
			pst.setString(1, key);
			if (conexion.Update(pst) > 0) {
				conexion.Commit();
				result = true;
			}
			
		return result;	
	}
	
	public boolean Find(String key) throws SQLException {
		boolean result = false;
			String sql = String.format ("select * from clientes where nic='%s'",key);
			ResultSet rs = conexion.Query(sql);
			if (rs.next()) {
				this.nic = (String)rs.getString("nic");
				this.longitud  = (String)rs.getString("longitud");
				this.latitud = (String)rs.getString("latitud");
				result = true;
			}
			rs.close();
			
		return result;	
	}
	
	public ResultSet List() throws SQLException {
		ResultSet result = null;
			String sql = "select * from clientes order by nic";
			result = conexion.Query(sql);
			return result;	
	}
	
}
